package com.itheima.util;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;

import java.util.Optional;

/**
 * @author GrantZhu
 * @Description
 * @create 2024-06-22 9:15
 */
public class PrintUtils {

    public static final String defaultComment = "看看容器中有哪些bean";


    public static void printBeanInFactory(DefaultListableBeanFactory beanFactory) {
        printBeanInFactory(beanFactory, defaultComment);
    }

    public static void printBeanInFactory(ApplicationContext beanFactory) {
        printBeanInFactory(beanFactory, defaultComment);
    }

    public static void printBeanInFactory(DefaultListableBeanFactory beanFactory, String comment) {
        System.out.println("--------------begin-----------------");
        System.out.println("comment: " + Optional.ofNullable(comment).orElse(defaultComment));
        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println("name = " + name);
        }
        System.out.println("---------------end----------------");
        System.out.println();
    }

    public static void printBeanInFactory(ApplicationContext beanFactory, String comment) {
        System.out.println("--------------begin-----------------");
        System.out.println("comment: " + Optional.ofNullable(comment).orElse(defaultComment));
        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println("name = " + name);
        }
        System.out.println("---------------end----------------");
        System.out.println();
    }
}
